package com.xomily.devops.service.impl;

import java.util.List;
import com.xomily.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xomily.devops.mapper.DevAlarmLogMapper;
import com.xomily.devops.mapper.DevAlarmMapper;
import com.xomily.devops.domain.DevAlarm;
import com.xomily.devops.domain.DevAlarmLog;
import com.xomily.devops.service.IDevAlarmLogService;

/**
 * 系统运行错误日志Service业务层处理
 * 
 * @author xomily
 * @date 2021-11-29
 */
@Service
public class DevAlarmLogServiceImpl implements IDevAlarmLogService 
{
    @Autowired
    private DevAlarmLogMapper devAlarmLogMapper;
    @Autowired
    private DevAlarmMapper devAlarmMapper;

    /**
     * 查询系统运行错误日志
     * 
     * @param id 系统运行错误日志主键
     * @return 系统运行错误日志
     */
    @Override
    public DevAlarmLog selectDevAlarmLogById(Long id)
    {
        return devAlarmLogMapper.selectDevAlarmLogById(id);
    }

    /**
     * 查询系统运行错误日志列表
     * 
     * @param devAlarmLog 系统运行错误日志
     * @return 系统运行错误日志
     */
    @Override
    public List<DevAlarmLog> selectDevAlarmLogList(DevAlarmLog devAlarmLog)
    {
        return devAlarmLogMapper.selectDevAlarmLogList(devAlarmLog);
    }

    /**
     * 新增系统运行错误日志
     * 
     * @param devAlarmLog 系统运行错误日志
     * @return 结果
     */
    @Override
    public int insertDevAlarmLog(DevAlarmLog devAlarmLog)
    {
    	DevAlarm alarmQuery=new DevAlarm();
    	alarmQuery.setIp(devAlarmLog.getIp());
    	alarmQuery.setCode(devAlarmLog.getCode());
    	DevAlarm alarm=devAlarmMapper.selectDevAlarmByIpCode(alarmQuery);
        if ( alarm!=null) {
        	alarm.setStatus("0");
        	alarm.setAlarmTime(DateUtils.getNowDate());
        	devAlarmMapper.updateDevAlarm(alarm);       
        } else {
        	alarm = new DevAlarm();
        	alarm.setIp(devAlarmLog.getIp());
        	alarm.setCode(devAlarmLog.getCode());
        	alarm.setType(devAlarmLog.getType());
        	alarm.setContent(devAlarmLog.getContent());
         	alarm.setStatus("0");
        	alarm.setCreateTime(DateUtils.getNowDate());
        	alarm.setAlarmTime(DateUtils.getNowDate());
        	devAlarmMapper.insertDevAlarm(alarm);               	
        }
        devAlarmLog.setCreateTime(DateUtils.getNowDate());
        return devAlarmLogMapper.insertDevAlarmLog(devAlarmLog);
    }

    
    /**
     * 修改系统运行错误日志
     * 
     * @param devAlarmLog 系统运行错误日志
     * @return 结果
     */
    @Override
    public int updateDevAlarmLog(DevAlarmLog devAlarmLog)
    {
        return devAlarmLogMapper.updateDevAlarmLog(devAlarmLog);
    }

    /**
     * 批量删除系统运行错误日志
     * 
     * @param ids 需要删除的系统运行错误日志主键
     * @return 结果
     */
    @Override
    public int deleteDevAlarmLogByIds(Long[] ids)
    {
        return devAlarmLogMapper.deleteDevAlarmLogByIds(ids);
    }

    /**
     * 删除系统运行错误日志信息
     * 
     * @param id 系统运行错误日志主键
     * @return 结果
     */
    @Override
    public int deleteDevAlarmLogById(Long id)
    {
        return devAlarmLogMapper.deleteDevAlarmLogById(id);
    }
}
